<template>
	<el-container>
		<el-header class="header-tabs">
			<el-tabs type="card" v-model="status" @tab-change="tabChange">
				<el-tab-pane label="全部" name="0"></el-tab-pane>
				<el-tab-pane label="展示中" name="1"></el-tab-pane>
				<el-tab-pane label="已下架" name="2"></el-tab-pane>
			</el-tabs>
		</el-header>
		<el-header style="height: auto;">
			<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
                <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
				<el-button type="info" icon="el-icon-refresh" @click="refresh"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="商品名称/简介" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" row-key="id"  @selection-change="selectionChange" stripe>
                <el-table-column type="selection" width="50"></el-table-column>
                <el-table-column label="封面图" prop="goods" width="95">
                    <template #default="scope">
                        <el-image class="feng2" :src="scope.row.feng_thumb" :fit="fit" />
                    </template>

                </el-table-column>
				<el-table-column label="标题" prop="title"></el-table-column>
                <el-table-column label="分类" prop="title" width="150" align="center">
                    <template #default="scope">
                        <el-tag type="primary">{{ scope.row.name }}</el-tag>
                    </template>

                </el-table-column>
				<el-table-column label="价格" prop="price" width="140" align="center">
						<template #default="scope">
						    <text style="color:#ff0000">{{ scope.row.price }}</text>
							<text style="text-decoration:line-through;color:#999;margin-left: 5px;" v-if="scope.row.origin_price > 0">{{ scope.row.origin_price }}</text>
						</template>
				</el-table-column>
                <el-table-column label="库存" prop="stock" width="120"  align="center"></el-table-column>
				<el-table-column label="销量" prop="sell_num" width="120"  align="center"></el-table-column>
                <el-table-column label="浏览数" prop="hits" width="120"  align="center"></el-table-column>
				<el-table-column label="状态" prop="status" width="110"  align="center">
                    <template #default="scope">
						<el-switch v-model="scope.row.status" @change="changeSwitch($event, scope.row,'status')" :loading="scope.row.$switch_status" active-value="1" inactive-value="0"></el-switch>
					</template>
                </el-table-column>
                <el-table-column label="推荐" prop="is_tj" width="100"  align="center">
                    <template #default="scope">
						<el-switch v-model="scope.row.is_tj" @change="changeSwitch($event, scope.row,'is_tj')" :loading="scope.row.$switch_status" active-value="1" inactive-value="0"></el-switch>
					</template>
                </el-table-column>
                <el-table-column label="置顶" prop="is_top" width="110"  align="center">
                    <template #default="scope">
						<el-switch v-model="scope.row.is_top" @change="changeSwitch($event, scope.row,'is_top')" :loading="scope.row.$switch_status" active-value="1" inactive-value="0"></el-switch>
					</template>
                </el-table-column>
				<el-table-column label="操作" fixed="right" align="center" width="160">
					<template #default="scope">
						<el-button-group>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
                            <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>

	</el-container>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
	import saveDialog from './save'
    import scSelectFilter from '@/components/scSelectFilter'
	export default {
		name: 'goods',
		components: {
			saveDialog,
            scSelectFilter
		},
		data() {
			return {
                selection: [],
				filterData: [
					{
						title: "商品分类",
						key: "cid",
						multiple: false,
						options: []
					}
				],
                dialog: {
					save: false,
					permission: false
				},
				list: {
					 apiObj: this.$API.system.goods.list
				},
                status:0,
                cid:'',
				search: {
					keyword: ""
				}
			}
		},
        mounted() {
			 this.getcat()
			 //
		},
		methods: {
            async getcat(){
				var res = await this.$API.system.universal.list.get({},'/goods/cat');
				if(res.code == 200){
                        var arr = res.data.rows
                        this.filterData[0].options.push({'label':'全部','value':''})
                        for(var i=0;i<arr.length;i++){
                            this.filterData[0].options.push({'label':arr[i].name,'value':arr[i].cid})
                        }

				}
			},
            async changeSwitch(e,row,t=''){
                var data;
                switch (t) {
                    case 'status':
                        data = {'gid':row.gid,'status':e}
                        break;
                    case 'is_tj':
                        data = {'gid':row.gid,'is_tj':e}
                        break;
                    case 'is_top':
                        data = {'gid':row.gid,'is_top':e}
                        break;
                    default:
                        break;
                }
                //var res = await this.$API.demo.goods_post.post(data);
				var res = await this.$API.system.universal.handle.post(data,'/goods/goods_save');
                this.isSaveing = false;
                if(res.code == 200){
                    this.$message.success("操作成功")
                }else{
                    this.$alert(res.message, "提示", {type: 'error'})
                }
            },
            //添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
            //删除
			async table_del(row){
				var reqData = {gid: row.gid}
				//var res = await this.$API.demo.goods_del.post(reqData);
				var res = await this.$API.system.universal.handle.post(reqData,'/goods/goods_del');
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
            //表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
            //批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
					type: 'warning'
				}).then(() => {
					var arr = this.selection
					var ob = []
					for(var i=0;i<arr.length;i++){
						  ob.push(arr[i].gid)
					}
					this.table_del({'gid':ob.join(",")})
				}).catch(() => {

				})
			},
			//搜索
			upsearch(){
				this.$refs.table.upData(this.search)
			},
			//标签切换
			tabChange(name){
                this.status = name
                this.$refs.table.reload({'cid':this.cid,'status':this.status})
			},
			filterChange(data){
                this.cid = data.cid
                this.$refs.table.reload({'cid':this.cid,'status':this.status})
				//this.$refs.table.upData(data)
			},
            refresh(){
                this.$refs.table.reload()
            },
            	//本地更新数据
			handleSaveSuccess(data, mode){
				if(mode=='add'){
					this.$refs.table.reload()
				}else if(mode=='edit'){
					this.$refs.table.reload()
				}
			}
		}

	}
</script>

<style>
.pro{
    width:100%;
    float: left;
}
.feng2{
    widows:40px;
    height:40px;
    border-radius: 5px;
}
.protitle{
    width: 80%;
    float: left;
    padding-left: 10px;
}
</style>
